package com.lanhai.crowd.mvc.handler;

import com.lanhai.crowd.entity.Auth;
import com.lanhai.crowd.entity.Role;
import com.lanhai.crowd.service.api.AdminService;
import com.lanhai.crowd.service.api.AuthService;
import com.lanhai.crowd.service.api.RoleService;
import com.lanhai.crowd.util.ResultEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
public class AssignController {

    @Autowired
    private RoleService roleService;
    @Autowired
    private AuthService authService;

    Logger log = LoggerFactory.getLogger(AssignController.class);



    @RequestMapping("/assign/to/admin/page.html")
    public String assginRolePage(
            @RequestParam("adminId") Integer adminId,
            ModelMap modelMap) {

        // 1.查询已分配角色
        List<Role> assignedRole = roleService.getAssignedRole(adminId);
        // 2.查询未分配角色
        List<Role> unassignedRole = roleService.getUnassignedRole(adminId);

        // 3.存入模型
        modelMap.addAttribute("assignedRoleList", assignedRole);
        modelMap.addAttribute("unassignedRoleList", unassignedRole);

        return "assign-role";
    }


    @RequestMapping("/assign/do/role.html")
    public String assignRole(
            @RequestParam("adminId") Integer adminId,
            @RequestParam(value = "roleIdList", required = false) List<Integer> roleIdList,
            @RequestParam("pageNum") Integer pageNum,
            @RequestParam("keyword") String keyword) {

        // 保存admin role 关系
        roleService.saveAdminRoleRelationship(adminId, roleIdList);

        for (Integer roleId : roleIdList) {
            log.info("传过来的roleId:" + roleId);
        }

        return "redirect:/admin/get/page.html?pageNum=" + pageNum
                + "&keyword=" + keyword;
    }

    @ResponseBody
    @RequestMapping("/assign/get/all/auth.json")
    public ResultEntity<List<Auth>> getAllAuth() {

        List<Auth> authList = authService.getAll();

        return ResultEntity.data(authList);
    }

    @ResponseBody
    @RequestMapping("/assign/get/assigned/auth.json")
    public ResultEntity<List<Integer>> getAssignedAuth(
            @RequestParam("roleId") Integer roleId) {

        List<Integer> authList = authService.getAssignedAuth(roleId);

        return ResultEntity.data(authList);
    }

    @ResponseBody
    @RequestMapping("/assign/do/role/assign.json")
    public ResultEntity<String> saveAuthRelationship(
            @RequestBody Map<String, List<Integer>> map) {

        authService.saveAuthRelationship(map);

        return ResultEntity.ok();
    }


}
